import { BasicColumn } from "/@/components/Table"
import { FormSchema } from "/@/components/Table"
import { h } from "vue"
import { Icon } from "/@/components/Icon"
import { ajaxGetDictItems } from "./api"
import { render } from "/@/utils/common/renderUtils"
const isRootMenu = (type) => type === "menu"
const isSubMenu = (type) => type === "1"
const isLink = (type) => type === "link_page" || type === "link_group"

// 定义可选择的组件类型
export enum ComponentTypes {
  Default = "layouts/default/index",
  IFrame = "sys/iframe/FrameBlank",
}

export const columns: BasicColumn[] = [
  {
    title: "页面名称",
    dataIndex: "name",
    width: 160,
    align: "left",
  },
  {
    title: "唯一标识",
    dataIndex: "code",
    width: 160,
  },
  {
    title: "链接类型",
    dataIndex: "typeText",
    width: 80,
    customRender: ({ text }) => {
      return render.renderTag(text.split(","), "green")
    },
    // customRender: ({text}) => {
    //   return  render.renderDict(text, 'menu_type')
    // }
  },
  {
    title: "页面链接",
    dataIndex: "pageUrl",
    align: "left",
    width: 200,
  },
  {
    title: "排序",
    dataIndex: "sortNo",
    width: 50,
  },
]

export const searchFormSchema: FormSchema[] = [
  {
    field: "name",
    label: "页面名称",
    component: "Input",
  },
  {
    field: "type",
    label: "类型",
    component: "JDictSelectTag",
    componentProps: {
      dictType: "diy_app_link_type",
      // type: 'radio',
    },
  },
]

export const formSchema: FormSchema[] = [
  {
    label: "id",
    field: "id",
    component: "Input",
    show: false,
  },
  {
    label: "parentMenuType",
    field: "parentMenuType",
    component: "Input",
    show: false,
  },
  {
    field: "type",
    label: "链接类型",
    component: "RadioButtonGroup",
    defaultValue: "menu",
    componentProps: ({ formModel, formActionType }) => {
      const isUpdate = !!formModel.id
      const isOneLevel = formModel.parentId && formModel.parentId === "0"
      const parentMenuType = formModel.parentMenuType
      return {
        options: [
          { label: "菜单", value: "menu" },
          { label: "链接分组", value: "link_group" },
          { label: "链接页面", value: "link_page" },
        ],
        onChange: (e) => {
          const { updateSchema, clearValidate } = formActionType
          //清除校验
          clearValidate()
          updateSchema([
            {
              field: "pageUrl",
              required: isLink(e),
            },
            // {
            //   field: 'parentId',
            //   show: isLink(e)
            // }
          ])
        },
      }
    },
  },
  {
    field: "name",
    label: "链接名称",
    component: "Input",
    required: true,
  },
  {
    field: "code",
    label: "唯一标识",
    component: "Input",
    required: true,
  },
  {
    field: "parentId",
    label: "上级",
    component: "MyTreeSelect",
    required: false,
    componentProps: {
      fieldNames: {
        title: "name",
        key: "id",
        value: "id",
      },
    },
    // ifShow: ({ values }) => !isLink(values.type),
  },
  {
    field: "pageUrl",
    label: "页面链接",
    component: "Input",
    required: false,
    helpMessage: ["eg: /pages/goods/list"],
  },
  {
    field: "component",
    label: "组件",
    component: "Input",
    defaultValue: "LinkData.vue",
    helpMessage: [
      "内置组件1: LinkData.vue (存放链接数据)",
      "内置组件2: Other.vue (其他, eg: 拨打电话)",
      "扩展组件3: 需要定义在views目录下, 且需要添加完整路径, eg /bizShop/Test.vue",
    ],
    ifShow: ({ values }) => !isLink(values.type),
  },

  {
    field: "status",
    label: "状态",
    component: "JDictSelectTag",
    defaultValue: "1",
    required: true,
    componentProps: {
      type: "radio",
      dictType: "data_use_status",
    },
  },
  {
    field: "sortNo",
    label: "排序",
    component: "InputNumber",
    defaultValue: 1,
  },
  // {
  //   field: 'alwaysShow',
  //   label: '聚合路由',
  //   component: 'Switch',
  //   colProps: { span: 24 },
  //   defaultValue: 0,
  //   componentProps: {
  //     checkedChildren: "是",
  //     unCheckedChildren: "否"
  //   },
  //   ifShow: ({ values }) => !isLink(values.type),
  // },
]
